home *** CD-ROM | disk | FTP | other *** search
-
- #include <windows.h>
- #include <windowsx.h>
- #include <mmsystem.h>
- #include <toolhelp.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "mci.h"
- #include "cd.h"
-
- #define TIME_NONE -999999
- #define INI_NAME "cdplay.ini"
- #define INI_TOPIC_NAME "Names"
- #define INI_TOPIC_SETTINGS "Settings"
- #define INI_POSITION "Position"
- #define MAX_CDNAME 64
-
- HINSTANCE hInst;
- char szAppName[] = "CD Player";
- BOOL CALLBACK CdDlgProc(HWND hwnd, UINT message,
- WPARAM wParam, LPARAM lParam);
- BOOL CALLBACK TitleDlgProc(HWND hwnd, UINT message,
- WPARAM wParam, LPARAM lParam);
-
- enum CDMode { ModeNotReady, ModePause, ModePlaying, ModeStop, ModeScanning };
-
- class CDPlayer : public CDAudio {
- HWND hwnd;
- int nTracks;
- DWORD *PosTracks; // Positionen der einzelnen Tracks in Millisekunden
- DWORD *LenTracks; // LΣngen der einzelnen Tracks in Millisekunden
- int Track; // aktueller Track
- DWORD Sync; // Systemzeit, bei dem das nΣchste "Sync" stattfindet
- DWORD StartTime; // Systemzeit, bei dem die CD gestartet wurde
- CDMode mode; // current status of drive
- DWORD dwPosition; // always contains the disc position in ms
- WORD wTimer; // ID of timer used to update display
- char CDTitle[MAX_CDNAME]; // Titel der CD
- char CDID[10]; // ID-String der CD
- BOOL bCountUp; // count seconds up or down
-
- protected:
- DWORD Time();
- int Synchronize(DWORD time = 0);
- void DisplayTime(LONG time, WORD wID);
- void UpdateDisplay(DWORD wTime);
- void CheckReady();
- void SetTrack(int wTrack);
- void SetPosition(DWORD dwPos);
-
- public:
- int Timer();
- int Login(); // Betimmt LΣnge der Tracks etc.
- int Logout(); // Setzt die Parameter zurⁿck
- int Resync();
- int Forward();
- int Back();
- int Pause();
- int Play();
- int Stop();
- void ToggleUp();
- void EditTitle();
- CDPlayer(HWND aWindow);
- ~CDPlayer();
- };
-
-
- char *String(UINT id)
- {
- static char szString[128];
-
- if (LoadString(hInst, id, szString, sizeof szString))
- return szString;
- else
- return "";
- }
-
-
- DWORD MciError(HWND hwnd, DWORD d)
- {
- char s[256];
-
- if (d) {
- mciGetErrorString(d, s, sizeof s);
- MessageBox(hwnd, s, szAppName, MB_OK);
- }
- return d;
- }
-
-
- CDPlayer::CDPlayer(HWND aWindow) : CDAudio(aWindow)
- {
- nTracks = 0;
- LenTracks = NULL;
- mode = ModeNotReady;
- Track = 0;
- hwnd = aWindow;
- Sync = 0;
- dwPosition = 0;
- bCountUp = TRUE;
- wTimer = SetTimer(hwnd, 1, 1000, NULL);
- }
-
-
- CDPlayer::~CDPlayer()
- {
- if (wTimer)
- KillTimer(hwnd, 1);
- }
-
-
- DWORD CDPlayer::Time()
- {
- return GetTickCount();
- /*
- TIMERINFO ti;
-
- ti.dwSize = sizeof ti;
- TimerCount(&ti);
- return ti.dwmsSinceStart;
- */
- }
-
-
- int CDPlayer::Synchronize(DWORD time)
- {
- int t;
- BOOL bPlay;
-
- if (time == 0)
- time = Time();
-
- Sync = time + 5000;
-
- SetTimeFormat(MCI_FORMAT_MILLISECONDS);
- bPlay = Mode() == MCI_MODE_PLAY;
-
- if (mode == ModeNotReady) {
- dwPosition = 0;
- Track = 0;
- }
-
- if (mode == ModePlaying && !bPlay) {
- // CD has stopped
- dwPosition = PosTracks[1];
- Track = 0;
- mode = ModeStop;
- }
- if (bPlay)
- mode = ModePlaying;
-
- if (mode == ModePlaying) {
- dwPosition = Position();
- Track = CurrentTrack();
- StartTime = time - dwPosition;
- if (Sync > StartTime + PosTracks[Track] + LenTracks[Track])
- Sync = StartTime + PosTracks[Track] + LenTracks[Track];
- }
- return TRUE;
- }
-
-
- void CDPlayer::DisplayTime(LONG time, WORD wID)
- {
- char buff[20];
- char buff2[20];
-
- if (time == TIME_NONE) {
- SetDlgItemText(hwnd, wID, " --:--");
- return;
- }
-
- if (time >= 0) {
- time /= 1000;
- wsprintf(buff, " %02d:%02d", int(time / 60), int(time % 60));
- } else {
- time = -(time / 1000) + 1;
- wsprintf(buff, "-%02d:%02d", int(time / 60), int(time % 60));
- }
- GetDlgItemText(hwnd, wID, buff2, sizeof buff2);
- if (strcmp(buff, buff2))
- SetDlgItemText(hwnd, wID, buff);
- }
-
-
- void CDPlayer::CheckReady()
- {
- BOOL bReady = Ready();
-
- if (bReady && mode == ModeNotReady) {
- // CD-Laufwerk ist gerade Ready geworden
- HCURSOR hOldCur = SetCursor(LoadCursor(NULL, IDC_WAIT));
- mode = ModeScanning;
- UpdateDisplay(dwPosition);
- Login();
- SetCursor(hOldCur);
- mode = ModeStop;
- Sync = 0;
- }
-
- if (!bReady && mode != ModeNotReady ) {
- // Laufwerk wurde gerade "gest÷rt"
- Logout();
- mode = ModeNotReady;
- Sync = 0;
- }
- }
-
-
- int CDPlayer::Resync()
- {
- CheckReady();
- Synchronize();
- UpdateDisplay(dwPosition);
- if (wTimer)
- KillTimer(hwnd, 1);
- wTimer = SetTimer(hwnd, 1, 1000, NULL);
- return TRUE;
- }
-
-
- void CDPlayer::UpdateDisplay(DWORD dwTime)
- {
- static char buff[MAX_CDNAME];
- char *s; // title string to display
- int t; // track number to display
- static UINT modes[] = {
- IDS_NOTREADY,
- IDS_PAUSED,
- IDS_PLAYING,
- IDS_STOPPED,
- IDS_SCANNING
- };
-
- t = Track;
- s = *CDTitle ? CDTitle : String(modes[mode]);
- if (mode == ModeNotReady || mode == ModeScanning) {
- t = 0;
- s = String(modes[mode]);
- DisplayTime(TIME_NONE, ID_SINGLETIME);
- DisplayTime(TIME_NONE, ID_TOTALTIME);
- } else if (Track == 0) {
- t = nTracks;
- DisplayTime(0, ID_SINGLETIME);
- DisplayTime(PosTracks[nTracks+1], ID_TOTALTIME);
- } else {
- if (bCountUp) {
- DisplayTime(dwTime - PosTracks[Track], ID_SINGLETIME);
- DisplayTime(dwTime - PosTracks[1], ID_TOTALTIME);
- } else {
- DisplayTime(PosTracks[Track+1] - dwTime, ID_SINGLETIME);
- DisplayTime(PosTracks[nTracks+1] - dwTime, ID_TOTALTIME);
- }
- }
-
- if (GetDlgItemInt(hwnd, ID_TRACKCOUNT, NULL, FALSE) != t)
- SetDlgItemInt(hwnd, ID_TRACKCOUNT, t, FALSE);
-
- GetDlgItemText(hwnd, ID_TITLE, buff, sizeof(buff));
- if (strcmp(s, buff))
- SetDlgItemText(hwnd, ID_TITLE, s);
- }
-
-
- int CDPlayer::Timer()
- {
- CheckReady();
-
- DWORD dwTime = Time();
- if (dwTime >= Sync) {
- // Synchronisation required
- Synchronize(dwTime);
- }
-
- if (mode == ModePlaying)
- dwPosition = dwTime - StartTime;
- UpdateDisplay(dwPosition);
- return TRUE;
- }
-
-
- void CDPlayer::SetTrack(int aTrack)
- {
- Track = aTrack;
- SetPosition(PosTracks[Track]);
- }
-
-
- void CDPlayer::SetPosition(DWORD dwPos)
- {
- dwPosition = dwPos;
- if (mode == ModePlaying) {
- SetTimeFormat(MCI_FORMAT_MILLISECONDS);
- MciError(hwnd, CDAudio::Play(MCI_FROM, dwPosition));
- }
- Resync();
- }
-
-
-
- int CDPlayer::Forward()
- {
- int w = Track + 1;
- if (w > nTracks)
- return TRUE;
- SetTrack(w);
- return TRUE;
- }
-
-
- int CDPlayer::Back()
- {
- int w = Track - 1;
- if (w < 1)
- return TRUE;
- SetTrack(w);
- return TRUE;
- }
-
-
- int CDPlayer::Pause()
- {
- if (mode == ModePlaying) {
- SetTimeFormat(MCI_FORMAT_MILLISECONDS);
- dwPosition = Position();
- CDAudio::Stop();
- }
- mode = ModePause;
- Resync();
- return TRUE;
- }
-
-
- int CDPlayer::Play()
- {
- if (mode == ModePlaying) {
- // if playing, play restarts the current song
- dwPosition = PosTracks[Track];
- }
- SetTimeFormat(MCI_FORMAT_MILLISECONDS);
- MciError(hwnd, CDAudio::Play(MCI_FROM, dwPosition));
- Resync();
- return TRUE;
- }
-
-
- int CDPlayer::Stop()
- {
- CDAudio::Stop();
- mode = ModeStop;
- SetTrack(0);
- return TRUE;
- }
-
-
-
- int CDPlayer::Logout()
- {
- EnableWindow(GetDlgItem(hwnd, ID_EDIT), FALSE);
- if (LenTracks) {
- delete LenTracks;
- delete PosTracks;
- LenTracks = NULL;
- }
- nTracks = 0;
- Track = 0;
- *CDTitle = '\0';
- return TRUE;
- }
-
-
- int CDPlayer::Login()
- {
- int i;
-
- Logout();
- if (!Ready())
- return FALSE;
-
- nTracks = NumberOfTracks();
- LenTracks = new DWORD[nTracks+1];
- PosTracks = new DWORD[nTracks+2];
-
- SetTimeFormat(MCI_FORMAT_FRAMES);
- long n=Length();
- SetTimeFormat(MCI_FORMAT_MILLISECONDS);
- n=Length();
-
- SetTimeFormat(MCI_FORMAT_MILLISECONDS);
- for (i = 1; i <= nTracks; ++i) {
- PosTracks[i] = Position(i);
- }
- PosTracks[0] = PosTracks[1];
-
- LenTracks[0] = 0;
- for (i = 1; i < nTracks; ++i) {
- LenTracks[i] = PosTracks[i+1] - PosTracks[i];
- }
- LenTracks[nTracks] = Length(i);
- PosTracks[nTracks+1] = PosTracks[nTracks] + LenTracks[nTracks];
- dwPosition = PosTracks[1];
- Track = 0;
-
- wsprintf(CDID, "%08lx", n);
- GetPrivateProfileString(INI_TOPIC_NAME, CDID, String(IDS_DEFAULTNAME),
- CDTitle, sizeof CDTitle, INI_NAME);
-
- if (!Ready()) {
- Logout();
- return FALSE;
- }
- EnableWindow(GetDlgItem(hwnd, ID_EDIT), TRUE);
- return TRUE;
- }
-
-
- void CDPlayer::EditTitle()
- {
- if (DialogBoxParam(hInst, MAKEINTRESOURCE(CDTitleDlg), hwnd,
- TitleDlgProc, (LPARAM)CDTitle)) {
- WritePrivateProfileString(INI_TOPIC_NAME, CDID, CDTitle, INI_NAME);
- Resync();
- }
- }
-
-
- void CDPlayer::ToggleUp()
- {
- bCountUp ^= 1;
- Resync();
- }
-
-
- int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASS wc;
- HWND hwnd;
- MSG msg;
- HINSTANCE hBwcc;
-
- hInst = hInstance;
-
- SetErrorMode(SEM_NOOPENFILEERRORBOX);
- hBwcc = LoadLibrary("BWCC.DLL");
- if ((UINT)hBwcc < 32) {
- MessageBox(NULL, String(IDS_NO_BWCC), szAppName,
- MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
-
- hwnd = CreateDialog(hInst, MAKEINTRESOURCE(CDPlayerDlg), NULL, CdDlgProc);
- if (!hwnd)
- return 0;
-
- SetClassWord(hwnd, GCW_HICON, (WORD)LoadIcon(hInst, MAKEINTRESOURCE(CDIcon)));
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
-
- /* Message Loop */
- while (GetMessage(&msg, NULL, NULL, NULL)) {
- if (!IsDialogMessage(hwnd, &msg)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- FreeLibrary(hBwcc);
- return msg.wParam;
- }
-
-
-
- BOOL CALLBACK _export CdDlgProc(HWND hwnd, UINT message,
- WPARAM wParam, LPARAM lParam)
- {
- static CDPlayer *cd;
-
- switch (message) {
-
- case WM_INITDIALOG: {
- int x, y;
- char szPos[20];
-
- GetPrivateProfileString(INI_TOPIC_SETTINGS, INI_POSITION, "", szPos,
- sizeof szPos, INI_NAME);
- if (sscanf(szPos, "%d %d", &x, &y) == 2)
- SetWindowPos(hwnd, NULL, x, y, 0, 0,
- SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
-
- SetWindowFont(GetDlgItem(hwnd, ID_SINGLETIME), GetStockObject(SYSTEM_FIXED_FONT), FALSE);
- SetWindowFont(GetDlgItem(hwnd, ID_TOTALTIME), GetStockObject(SYSTEM_FIXED_FONT), FALSE);
- EnableWindow(GetDlgItem(hwnd, ID_EDIT), FALSE);
- cd = new CDPlayer(hwnd);
- if (MciError(hwnd, cd->Open("Player", MCI_OPEN_SHAREABLE))) {
- DestroyWindow(hwnd);
- }
-
- return TRUE;
- }
-
- case WM_TIMER:
- cd->Timer();
- break;
-
- case MM_MCINOTIFY:
- cd->Resync();
- break;
-
- case WM_COMMAND:
- switch (wParam) {
-
- case ID_PLAY:
- cd->Play();
- break;
-
- case ID_STOP:
- cd->Stop();
- cd->Resync();
- break;
-
- case ID_PAUSE:
- cd->Pause();
- break;
-
- case ID_FORWARD:
- cd->Forward();
- cd->Resync();
- break;
-
- case ID_BACK:
- cd->Back();
- cd->Resync();
- break;
-
- case ID_QUIT:
- DestroyWindow(hwnd);
- break;
-
- case ID_EDIT:
- cd->EditTitle();
- break;
-
- case ID_UPDOWN:
- cd->ToggleUp();
- break;
- }
- break;
-
- case WM_CLOSE:
- DestroyWindow(hwnd);
- break;
-
- case WM_DESTROY: {
- RECT rc;
- char szPos[20];
-
- GetWindowRect(hwnd, &rc);
- wsprintf(szPos, "%d %d", rc.left, rc.top);
- WritePrivateProfileString(INI_TOPIC_SETTINGS, INI_POSITION, szPos, INI_NAME);
-
- if (cd)
- cd->Close();
- delete cd;
- PostQuitMessage(0);
- break;
- }
- default:
- return FALSE;
- }
- return TRUE;
- }
-
-
- BOOL CALLBACK _export TitleDlgProc(HWND hwnd, UINT message,
- WPARAM wParam, LPARAM lParam)
- {
- static char *CDTitle;
- static HWND hwndTitle;
-
- switch (message) {
-
- case WM_INITDIALOG:
- CDTitle = (char *)lParam;
- hwndTitle = GetDlgItem(hwnd, ID_TITLE);
- Edit_LimitText(hwndTitle, MAX_CDNAME-1);
- Edit_SetText(hwndTitle, CDTitle);
- return TRUE;
-
- case WM_COMMAND:
- switch (wParam) {
- case IDOK:
- Edit_GetText(hwndTitle, CDTitle, MAX_CDNAME);
- EndDialog(hwnd, TRUE);
- break;
-
- case IDCANCEL:
- EndDialog(hwnd, FALSE);
- break;
- }
- break;
-
- default:
- return FALSE;
- }
- return TRUE;
- }
-
-
-